package fr.cyol.android.levaldesetoiles;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class AutoRepeatButton extends Button {
	private static final int DEFAULT_INITIAL_DELAY = 500;
	private static final int DEFAULT_REPEAT_INTERVAL = 100;
	private long initialRepeatDelay = 500;
	private long repeatIntervalInMilliseconds = 100;

	public AutoRepeatButton(Context context, AttributeSet attrs) {
		super(context, attrs);

	    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoRepeatButton);
	    int n = a.getIndexCount();
	    for (int i = 0; i < n; i++) {
	        int attr = a.getIndex(i);

	        switch (attr) {
	        case R.styleable.AutoRepeatButton_initial_delay:
	            initialRepeatDelay = a.getInt(attr, DEFAULT_INITIAL_DELAY);
	            break;
	        case R.styleable.AutoRepeatButton_repeat_interval:
	            repeatIntervalInMilliseconds = a.getInt(attr, DEFAULT_REPEAT_INTERVAL);
	            break;
	        }
	    }
	    a.recycle();
		commonConstructorCode();
	}
	
	private Runnable repeatClickWhileButtonHeldRunnable = new Runnable() {
		public void run() {
			// Perform the present repetition of the click action provided by
			// the user in setOnClickListener().
			performClick();

			// Schedule the next repetitions of the click action, using a faster
			// repeat interval than the initial repeat delay interval.
			postDelayed(repeatClickWhileButtonHeldRunnable, repeatIntervalInMilliseconds);
		}
	};

	private void commonConstructorCode() {
		this.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				if (action == MotionEvent.ACTION_DOWN) {
					// Just to be sure that we removed all callbacks,
					// which should have occurred in the ACTION_UP
					removeCallbacks(repeatClickWhileButtonHeldRunnable);

					// Perform the default click action.
					performClick();

					// Schedule the start of repetitions after a one half second
					// delay.
					postDelayed(repeatClickWhileButtonHeldRunnable, initialRepeatDelay);
				} else if (action == MotionEvent.ACTION_UP) {
					// Cancel any repetition in progress.
					removeCallbacks(repeatClickWhileButtonHeldRunnable);
				}

				// Returning true here prevents performClick() from getting
				// called in the usual manner, which would be redundant,
				// given that we are already calling it above.
				return true;
			}
		});
	}

	public AutoRepeatButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		commonConstructorCode();
	}

	public AutoRepeatButton(Context context) {
		super(context);
		commonConstructorCode();
	}
}
